package cn.brondfish.icedfish.framework.util;

import android.util.Log;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.ConnectionSpec;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.TlsVersion;

import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class BF_HttpClient {
    private static volatile OkHttpClient okHttpClient;
    private static volatile boolean currentIsHttps = false; // Track the current configuration
    private static final int TIMEOUT = 30; // Timeout in seconds
    private static final String ERROR_TAG = "BF_HttpClient->Require Server Error";
    private BF_HttpClient() {}

    public static OkHttpClient getInstance(boolean isHttps) {
        if (okHttpClient == null || currentIsHttps != isHttps) {
            synchronized (BF_HttpClient.class) {
                if (okHttpClient == null || currentIsHttps != isHttps) {
                    // Rebuild the client if the configuration has changed
                    okHttpClient = buildClient(isHttps);
                    currentIsHttps = isHttps;
                }
            }
        }
        return okHttpClient;
    }

    private static OkHttpClient buildClient(boolean isHttps) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS);

        // Add a custom interceptor to handle HTTP/HTTPS protocol mismatches
        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (isHttps){
                    if ("http".equals(request.url().scheme())) {
                        Log.e(ERROR_TAG,"Attempted to send HTTP request while HTTPS is configured.");
                        throw new IOException("HTTP request attempted while HTTPS is configured.");
                    }
                } else {
                    if ("https".equals(request.url().scheme())) {
                        Log.e(ERROR_TAG,"Attempted to send HTTPS request while HTTP is configured.");
                        throw new IOException("HTTPS request attempted while HTTP is configured.");
                    }
                }
                return chain.proceed(request);
            }
        });

        if (isHttps) {
            // Configure for HTTPS requests
            ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_2)
                    .build();
            builder.connectionSpecs(Collections.singletonList(spec));
        } else {
            // Configure for HTTP requests with dynamic IP address
            ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT)
                    .build();
            builder.connectionSpecs(Collections.singletonList(spec));
        }

        return builder.build();
    }
}
